package gameObject;

import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import main.Core;
import action.PeriodicMovement;
import action.Transiction;
import constants.ImagesConstants;
import data.ImageSprite;

/**
 * 
 * @author Ygor
 * 
 */
public class Arrow extends ImageSprite {

	private PeriodicMovement periodicMov;
	private Transiction transiction;
	private int amountToMove; // o tanto que a arrow meche quando se move.
	private int position; // position can be 0 (startGame), 1 (load) and 2 (exit);
	private int numPosition;
	private int initialPositionX;
	private int initialPositionY;
	private boolean vertical = true;

	public Arrow(int x, int y, int w, int h, Core c, int numPosition) throws IOException {
		super(ImageIO.read(new File(ImagesConstants.ARROW)), x, y, w, h, c);
		periodicMov = new PeriodicMovement(6.6f, 1f);

		position = 0;
		initialPositionX = x;
		initialPositionY = y;
		this.numPosition = numPosition - 1;
	}

	@Override
	public void Update(double elapsedTime) {
		if (vertical) {
			setX((int) periodicMov.Update((double) initialPositionX,
					elapsedTime));
		} else {
			setY((int) periodicMov.Update((double) initialPositionY,
					elapsedTime));
		}

		if (null != transiction) {
			if (transiction.isJobFinished()) {
				transiction = null;
			} else {
				if (vertical) {
					this.setY((int) transiction.update(initialPositionY,
							elapsedTime));
				} else {
					this.setX((int) transiction.update(initialPositionX,
							elapsedTime));
				}
			}
		}
	}

	public void OnKeyPress(KeyEvent keyPressed) {
		if(vertical){
			if (keyPressed.getKeyCode() == KeyEvent.VK_DOWN) {
				if (position < numPosition) {					
					initialPositionY = this.getY();
					transiction = new Transiction(this.getY() + amountToMove,
							0.2f);
				} else {
					initialPositionX = this.getX();
					transiction = new Transiction(this.getX() + amountToMove,
							5.2f);
					transiction = new Transiction(this.getY() + amountToMove, 0.2f);
					position++;
				}
			} else if (keyPressed.getKeyCode() == KeyEvent.VK_UP) {
				if (position > 0) {					
					initialPositionY = this.getY();
					transiction = new Transiction(this.getY() - amountToMove, 0.2f);
					position--;
				}
			}
		}else{
			if (keyPressed.getKeyCode() == KeyEvent.VK_RIGHT) {
				initialPositionX = this.getX();
				transiction = new Transiction(this.getX() + amountToMove, 0.2f);
				position++;
			}else if (keyPressed.getKeyCode() == KeyEvent.VK_LEFT) {
				initialPositionX = this.getX();
				transiction = new Transiction(this.getX() - amountToMove, 0.2f);
				position--;
			}
		}
	}

	/**
	 * @return the periodicMov
	 */
	public PeriodicMovement getPeriodicMov() {
		return periodicMov;
	}

	/**
	 * @param periodicMov
	 *            the periodicMov to set
	 */
	public void setPeriodicMov(PeriodicMovement periodicMov) {
		this.periodicMov = periodicMov;
	}

	/**
	 * @return the position
	 */
	public int getPosition() {
		return position;
	}

	/**
	 * @param position
	 *            the position to set
	 */
	public void setPosition(int position) {
		this.position = position;
	}

	/**
	 * @return the numPosition
	 */
	public int getNumPosition() {
		return numPosition;
	}

	/**
	 * @param numPosition
	 *            the numPosition to set
	 */
	public void setNumPosition(int numPosition) {
		this.numPosition = numPosition;
	}

	/**
	 * @return the amountToMove
	 */
	public int getAmountToMove() {
		return amountToMove;
	}

	/**
	 * @param amountToMove
	 *            the amountToMove to set
	 */
	public void setAmountToMove(int amountToMove) {
		this.amountToMove = amountToMove;
	}

	/**
	 * @return the vertical
	 */
	public boolean isVertical() {
		return vertical;
	}

	/**
	 * @param vertical
	 *            the vertical to set
	 */
	public void setVertical(boolean vertical) {
		this.vertical = vertical;
	}

}
